package com.distribution.system.controller;

import com.distribution.common.annotation.RepeatSubmit;
import com.distribution.common.core.controller.BaseController;
import com.distribution.common.core.domain.AjaxResult;
import com.distribution.common.core.domain.entity.SysUser;
import com.distribution.common.core.page.TableDataInfo;
import com.distribution.common.utils.StringUtils;
import com.distribution.common.utils.poi.ExcelUtil;
import com.distribution.system.domain.bo.CommissionReflectionExportBO;
import com.distribution.system.domain.converter.CommissionReflectionConverter;
import com.distribution.system.domain.dto.AgentDTO;
import com.distribution.system.domain.dto.CmccDaCommissionReflectionDto;
import com.distribution.system.domain.dto.CommissionReflectionDto;
import com.distribution.system.domain.dto.ProcessCommissionReflectionDTO;
import com.distribution.system.domain.entity.CmccDaCommissionReflection;
import com.distribution.system.domain.req.CommissionReflectionListReq;
import com.distribution.system.domain.req.ReqResult;
import com.distribution.system.mapper.SysUserMapper;
import com.distribution.system.service.CmccDaCommissionReflectionService;
import com.distribution.system.service.ISysUserService;
import com.distribution.system.utils.convert.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (CmccDaCommissionReflection)表控制层
 */
@Validated
@RestController
@RequestMapping(value = "/cmccDaCommissionReflection")
@Slf4j
public class CmccDaCommissionReflectionController extends BaseController {
    private static final Logger agentWithdrawLog = LoggerFactory.getLogger("agentWithdrawLog");

    @Resource
    CmccDaCommissionReflectionService cmccDaCommissionReflectionService;
    @Resource
    SysUserMapper sysUserMapper;
    @Resource
    ISysUserService userService;
    @Resource
    CommissionReflectionConverter commissionReflectionConverter;

    /**
     * 查询提现记录列表
     */
    @PostMapping(value = "/selectCmccDaCommissionReflection")
    public TableDataInfo selectCmccDaCommissionReflection(@RequestBody CommissionReflectionListReq commissionReflectionReq){
        log.info("查询提现记录入参：{}", commissionReflectionReq.toString());
        agentWithdrawLog.info("查询提现记录入参：{}", commissionReflectionReq);
        List<CmccDaCommissionReflection> cmccDaCommissionReflections = new ArrayList<>();
        Long userId = getUserId();
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        List<Long> managerRoleIdList = Arrays.asList(1L,3L,103L);
        if (managerRoleIdList.contains(sysUser.getRoleId())) {
            AgentDTO agentDTO = new AgentDTO();
            agentDTO.setParentId(userId);
//            List<SysUser> sysUserList = sysUserMapper.selectAllUserListByParentId(agentDTO);
            List<SysUser> sysUserList = userService.selectUserListByParentId(agentDTO);
            List<Long> userIdList = sysUserList.stream().map(SysUser::getUserId).collect(Collectors.toList());
            startPage();
            cmccDaCommissionReflections = cmccDaCommissionReflectionService.lambdaQuery()
                    .eq(!Objects.isNull(commissionReflectionReq.getWithdrawalId()), CmccDaCommissionReflection::getId, commissionReflectionReq.getWithdrawalId())
                    .eq(!StringUtils.isBlank(commissionReflectionReq.getCreateAccount()), CmccDaCommissionReflection::getCreateAccount, commissionReflectionReq.getCreateAccount())
                    .eq(!StringUtils.isBlank(commissionReflectionReq.getCreateUserName()), CmccDaCommissionReflection::getCreateUserName, commissionReflectionReq.getCreateUserName())
                    .eq(!StringUtils.isBlank(commissionReflectionReq.getReflectionAccount()), CmccDaCommissionReflection::getReflectionAccount, commissionReflectionReq.getReflectionAccount())
                    .eq(!StringUtils.isBlank(commissionReflectionReq.getReflectionAccountName()), CmccDaCommissionReflection::getReflectionAccountName, commissionReflectionReq.getReflectionAccountName())
                    .eq(!Objects.isNull(commissionReflectionReq.getReflectionState()), CmccDaCommissionReflection::getReflectionState, commissionReflectionReq.getReflectionState())
                    .in(CmccDaCommissionReflection::getCreateUserId, userIdList)
                    .ge(!Objects.isNull(commissionReflectionReq.getBeginTime()), CmccDaCommissionReflection::getCreateTime, commissionReflectionReq.getBeginTime())
                    .le(!Objects.isNull(commissionReflectionReq.getEndTime()), CmccDaCommissionReflection::getCreateTime, commissionReflectionReq.getEndTime())
                    .orderByDesc(CmccDaCommissionReflection::getCreateTime)
                    .list();
            log.info("查询提现记录结果：{}", cmccDaCommissionReflections);
            agentWithdrawLog.info("查询提现记录结果：{}", cmccDaCommissionReflections);
            return getDataTable(cmccDaCommissionReflections);
        }else {
            cmccDaCommissionReflections = cmccDaCommissionReflectionService.lambdaQuery()
                    .eq(!Objects.isNull(commissionReflectionReq.getWithdrawalId()), CmccDaCommissionReflection::getId, commissionReflectionReq.getWithdrawalId())
                    .eq(CmccDaCommissionReflection::getCreateUserId, getUserId())
                    .orderByDesc(CmccDaCommissionReflection::getCreateTime).list();
            startPage();
            log.info("查询提现记录结果：{}", cmccDaCommissionReflections);
            agentWithdrawLog.info("查询提现记录结果：{}", cmccDaCommissionReflections);
            return getDataTable(cmccDaCommissionReflections);
        }
    }

    @PostMapping("/export")
    @PreAuthorize("@ss.hasAnyRoles('operationSupervisor,agencyOperation')")
    public void export(HttpServletResponse response, CommissionReflectionListReq commissionReflectionReq) {
        CmccDaCommissionReflectionDto cmccDaCommissionReflectionDto = commissionReflectionConverter.commissionReflectionListReqToDto(commissionReflectionReq);
        AgentDTO agentDTO = new AgentDTO();
        agentDTO.setParentId(getUserId());
//        List<SysUser> sysUserList = sysUserMapper.selectAllUserListByParentId(agentDTO);
        List<SysUser> sysUserList = userService.selectUserListByParentId(agentDTO);
        List<Long> userIdList = sysUserList.stream().map(SysUser::getUserId).collect(Collectors.toList());
        List<CmccDaCommissionReflection> cmccDaCommissionReflections = cmccDaCommissionReflectionService.lambdaQuery()
                .eq(!StringUtils.isBlank(cmccDaCommissionReflectionDto.getCreateAccount()), CmccDaCommissionReflection::getCreateAccount, cmccDaCommissionReflectionDto.getCreateAccount())
                .eq(!StringUtils.isBlank(cmccDaCommissionReflectionDto.getCreateUserName()), CmccDaCommissionReflection::getCreateUserName, cmccDaCommissionReflectionDto.getCreateUserName())
                .eq(!StringUtils.isBlank(cmccDaCommissionReflectionDto.getReflectionAccount()), CmccDaCommissionReflection::getReflectionAccount, cmccDaCommissionReflectionDto.getReflectionAccount())
                .eq(!StringUtils.isBlank(cmccDaCommissionReflectionDto.getReflectionAccountName()), CmccDaCommissionReflection::getReflectionAccountName, cmccDaCommissionReflectionDto.getReflectionAccountName())
                .eq(!Objects.isNull(cmccDaCommissionReflectionDto.getReflectionState()), CmccDaCommissionReflection::getReflectionState, cmccDaCommissionReflectionDto.getReflectionState())
                .in(CmccDaCommissionReflection::getCreateUserId, userIdList)
                .ge(!Objects.isNull(cmccDaCommissionReflectionDto.getBeginTime()), CmccDaCommissionReflection::getCreateTime, cmccDaCommissionReflectionDto.getBeginTime())
                .le(!Objects.isNull(cmccDaCommissionReflectionDto.getEndTime()), CmccDaCommissionReflection::getCreateTime, cmccDaCommissionReflectionDto.getEndTime())
                .orderByDesc(CmccDaCommissionReflection::getCreateTime)
                .list();
        List<CommissionReflectionExportBO> cmccDaOrderExportBOS = BeanUtils.copyBeanCollection(CommissionReflectionExportBO.class, cmccDaCommissionReflections);
        ExcelUtil<CommissionReflectionExportBO> util = new ExcelUtil<>(CommissionReflectionExportBO.class);
        util.exportExcel(response, cmccDaOrderExportBOS, "提现记录");
    }

    /**
     * 提现申请
     */
    @PostMapping(value = "/addCmccDaCommissionReflection")
    @RepeatSubmit()
    public ReqResult addCmccDaCommissionReflection(@RequestBody CommissionReflectionDto commissionReflectionDto){
        log.info("提现申请入参：{}", commissionReflectionDto.toString());
        agentWithdrawLog.info("提现申请入参：{}", commissionReflectionDto);
        return cmccDaCommissionReflectionService.addCmccDaCommissionReflection(commissionReflectionDto);
    }

    /**
     * 发送佣金提现验证码
     */
    @PostMapping(value = "/sendCommissionReflectionSmsCode")
    @RepeatSubmit()
    public ReqResult sendCommissionReflectionSmsCode(@RequestBody CommissionReflectionDto commissionReflectionDto){
        log.info("发送提现验证码入参：{}", commissionReflectionDto.toString());
        agentWithdrawLog.info("发送提现验证码入参：{}", commissionReflectionDto);
        return cmccDaCommissionReflectionService.sendCommissionReflectionSmsCode(commissionReflectionDto);
    }

    /**
     * 审核
     **/
    @PostMapping(value = "/processCmccDaCommissionReflection")
    @RepeatSubmit()
    public AjaxResult processCmccDaCommissionReflection(@RequestBody ProcessCommissionReflectionDTO processCommissionReflectionDTO){
        log.info("提现审核入参：{}", processCommissionReflectionDTO.toString());
        agentWithdrawLog.info("提现审核入参：{}", processCommissionReflectionDTO);
        cmccDaCommissionReflectionService.processCmccDaCommissionReflection(processCommissionReflectionDTO);
        return success();
    }

    /**
     * 添加审核备注
     **/
    @PostMapping(value = "/addAuditRemark")
    @RepeatSubmit()
    public AjaxResult addAuditRemark(@RequestBody @Valid ProcessCommissionReflectionDTO processCommissionReflectionDTO){
        log.info("添加审核备注入参：{}", processCommissionReflectionDTO.toString());
        agentWithdrawLog.info("添加审核备注入参：{}", processCommissionReflectionDTO);
        cmccDaCommissionReflectionService.lambdaUpdate()
                .eq(CmccDaCommissionReflection::getId, processCommissionReflectionDTO.getId())
                .set(CmccDaCommissionReflection::getRemark, processCommissionReflectionDTO.getRemark())
                .update();
        return success();
    }

    /**
    * @Author: GodLu
    * @Date: 2024/9/1 0:37
    * @Description: 查询提现审核列表
    * @param cmccDaCommissionReflectionDto 查询对象
    * @return: TableDataInfo
    */
    @PostMapping(value = "/selectProcessCmccDaCommissionReflection")
    public TableDataInfo selectProcessCmccDaCommissionReflection(@RequestBody CmccDaCommissionReflectionDto cmccDaCommissionReflectionDto){
        log.info("查询提现审核列表入参：{}", cmccDaCommissionReflectionDto.toString());
        agentWithdrawLog.info("查询提现审核列表入参：{}", cmccDaCommissionReflectionDto);
        startPage();
        List<CmccDaCommissionReflection> cmccDaCommissionReflections = cmccDaCommissionReflectionService.selectProcessCmccDaCommissionReflection(cmccDaCommissionReflectionDto);
        log.info("查询提现审核列表结果：{}", cmccDaCommissionReflections);
        agentWithdrawLog.info("查询提现审核列表结果：{}", cmccDaCommissionReflections);
        return getDataTable(cmccDaCommissionReflections);
    }
}
